from gi.repository import Gtk 
import sys, os, getpass
from os.path import expanduser
from time import localtime, strftime

def error_message(message):
    
        # log to terminal window
#        print (message)  # Debug entry
        
        # create an error message dialog and display modally to the user
        dialog = Gtk.MessageDialog(None,
                                   Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                   Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, message)
        
        dialog.run()
        dialog.destroy()
        
def info_message(message):
    
        # log to terminal window
#        print (message)  # Debug entry
        
        # create an info message dialog and display modally to the user
        dialog = Gtk.MessageDialog(None,
                                   Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                   Gtk.MessageType.INFO, Gtk.ButtonsType.OK, message)
        
        dialog.run()
        dialog.destroy()

def get_username():
    username = getpass.getuser()
    if username: return username
    else: return None

def get_userdir():    
    home = expanduser("~")
    if home: return home
    else: return None
    
def get_time():
    time = strftime("%Y-%m-%d %H:%M:%S", localtime())
    if time: return time
    else: return None
    
def get_date():
    date = time = strftime("%Y-%m-%d", localtime())
    if date: return date
    else: return None

def get_logfile():
    ''' Get path for log file, should probably be stored outside of user dir.
    
    But for now we do like this. Then we have no problem with writing access.'''
    home = get_userdir()
    logdir = home + "/.support"
    os.makedirs(logdir, exist_ok=True) # Create logdir if not exist.
    username = get_username()
    time = get_time()
    log_file = logdir + "/" + username + "_" + time + ".log"
    if log_file: return log_file
    else: return None
    

